package com.gxt.basic.service.impl;

import com.gxt.basic.mapper.BaseMapper;
import com.gxt.basic.query.BaseQuery;
import com.gxt.basic.service.IBaseService;
import com.gxt.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Override
    @Transactional
    public void deleteById(Serializable id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Serializable id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> pageList(BaseQuery baseQuery) {
        // 查询总条数
        long total = baseMapper.queryTotal(baseQuery);
        if(total < 1){
            return new PageList<>();
        }
        // 查询当前页数据
        List<T> departments = baseMapper.queryDatas(baseQuery);
        return new PageList<>(total, departments);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }
}
